package com.meihua.shellapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.adapter.HomeRVAdapter;
import com.meihua.shellapp.adapter.OnItemClickListener;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.HomeIndexBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * 搜索界面
 */

public class SearchActivity extends BaseActivity implements OnItemClickListener {

    @Bind(R.id.et_search)
    EditText etSearch;//搜索
    @Bind(R.id.ins_iRecyclerView)
    IRecyclerView mRecyclerView;
    private int mPage = 1;
    private List<HomeIndexBean.DataBean> mData;
    private HomeRVAdapter rvAdapter;
    public LoadMoreFooterView mLoadMoreFooterView;


    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        etSearch.setOnEditorActionListener(onEditorActionListener);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mData = new ArrayList<>();
        rvAdapter = new HomeRVAdapter(mData);
        rvAdapter.setOnItemClickListener(this);
        mRecyclerView.setIAdapter(rvAdapter);
        /*设置iv*/
        mLoadMoreFooterView = (LoadMoreFooterView) mRecyclerView.getLoadMoreFooterView();
        mRecyclerView.setOnRefreshListener(this);
        mRecyclerView.setOnLoadMoreListener(this);
    }

    /*输入法搜索监听*/
    private TextView.OnEditorActionListener onEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            mPage = 1;
            hiddenInputMethod();
            getServerData(textView.getText().toString());
            return false;
        }


    };

    /*搜索*/
    private void getServerData(final String text) {

        OkGo.<String>post(URL.getUrl().HOME_URL)
                .params("name", text)
                .params("page", mPage + "")
                .execute(new MyStringCallBack<HomeIndexBean>(mRecyclerView) {
                    @Override
                    public void onSuccessData(HomeIndexBean bean) {
                        super.onSuccessData(bean);
                        if (null != bean.getData() && bean.getData().size() != 0) {
                            if (mPage == 1) {
                                mData.clear();
                            }
                            mData.addAll(bean.getData());
                            rvAdapter.notifyDataSetChanged();
                        } else {
                            if (mPage == 1) {
                                mData.clear();
                                rvAdapter.notifyDataSetChanged();
                            }
                            mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
                        }
                    }
                });

        Map<String, String> params = new HashMap() {{
            put("name", text);
            put("page", mPage + "");
        }};
        NetHelper.post(URL.getUrl().HOME_URL, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                HomeIndexBean bean = MyApplication.getInstance().getBeanForJson(HomeIndexBean.class, response.body());
                if (bean == null) return;
                if (null != bean.getData() && bean.getData().size() != 0) {
                    if (mPage == 1) {
                        mData.clear();
                    }
                    mData.addAll(bean.getData());
                    rvAdapter.notifyDataSetChanged();
                } else {
                    if (mPage == 1) {
                        mData.clear();
                        rvAdapter.notifyDataSetChanged();
                    }
                    mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                mRecyclerView.setRefreshing(false);
            }
        }, true);
    }

    //*条目点击*//*
    @Override
    public void onItemClick(int position, View v) {
        Intent intent = new Intent(this, WebActivity.class);
        intent.putExtra("url", URL.getUrl().WEBPRODUCT + mData.get(position).getId() + "/" + MyApplication.getInstance().getMId());
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        mPage = 1;
        getServerData(etSearch.getText().toString());
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        if (mLoadMoreFooterView.canLoadMore() && rvAdapter.getItemCount() > 0) {
            mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
            mPage++;
            getServerData(etSearch.getText().toString());
        }
    }
}
